//
//  BaseViewController.m
//  HouseAgent
//
//  Created by 杨洋 on 14-10-26.
//  Copyright (c) 2014年 com.yy. All rights reserved.
//

#import "BaseViewController.h"

//#import "LoginVC.h"
#import "UIView+Toast.h"
//#import "LeaveMessageVC.h"
#import "OneLevelBaseViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    if (IOSVersion >= 7.0)
    {
      self.automaticallyAdjustsScrollViewInsets = NO;  
    }
}

-(void)removeAllSubViews:(UIView *)view
{
    for (UIView *subview in view.subviews) {
        if ([subview isKindOfClass:[UIView class]]) {
            [subview removeFromSuperview];
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)setupRefresh:(UITableView *)tableView type:(RefreshType)num
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    if (num == AllType)
    {
        [tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
        [tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    }else if(num == HeaderType){
        [tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    }else if (num == FooterType){
        [tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    }
}

-(void)headerRereshing
{
    
}

-(void)footerRereshing
{
    
}

-(CGSize)returnTextSize:(NSString *)text and:(CGFloat)width andF:(UIFont *)font
{
    
    return [self returnTextSize:text and:width andF:font andPar:0];
}

-(CGSize)returnTextSize:(NSString *)text and:(CGFloat)width andF:(UIFont *)font andPar:(CGFloat)spacing
{
    
    if (IsiOS7Later) {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        paragraphStyle.lineSpacing = spacing;
        NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
        return    [text boundingRectWithSize:Size(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    }else{
        return   [text sizeWithFont:font constrainedToSize:Size(width, CGFLOAT_MAX)];
    }
    
}

//-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    [self.view endEditing:YES];
//}


#pragma mark -  check phone Num
- (BOOL)checkTel:(NSString *)str

{
    if (!str || [str isEqualToString:@""]) {
        [self.view makeToast:@"手机号不能为空"];
        return NO;
    }
    if (str.length != 11) {
        [self.view makeToast:@"手机号必须为11位"];
        return NO;
    }
    
    NSString *regex = @"^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    BOOL isMatch = [pred evaluateWithObject:str];
    
    if (!isMatch) {
        [self.view makeToast:@"请输入正确的手机号"];
        
        return NO;
    }
    return YES;
    
}//isIDCard2=/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/;

-(BOOL)checkIdCardNum:(NSString *)str
{
    NSString *value =str;
    value = [value stringByReplacingOccurrencesOfString:@"X" withString:@"x"];
    value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    int length = 0;
    if (!value) {
        return NO;
    }else {
        length = (int)value.length;
        if (length != 15 && length !=18) {
            return NO;
        }
    }
    // 省份代码
    NSArray *areasArray =@[@"11", @"12", @"13", @"14", @"15", @"21", @"22", @"23", @"31", @"32", @"33", @"34", @"35", @"36", @"37", @"41", @"42", @"43", @"44", @"45", @"46", @"50", @"51", @"52", @"53", @"54", @"61", @"62", @"63", @"64", @"65", @"71", @"81", @"82", @"91"];
    NSString *valueStart2 = [value substringToIndex:2];
    BOOL areaFlag = NO;
    for (NSString *areaCode in areasArray) {
        if ([areaCode isEqualToString:valueStart2]) {
            areaFlag = YES;
            break;
        }
    }
    if (!areaFlag) {
        return NO;
    }
    NSRegularExpression *regularExpression;
    NSUInteger numberofMatch;
    int year = 0;
    switch (length) {
        case 15:
            year = [value substringWithRange:NSMakeRange(6,2)].intValue +1900;
            if (year % 4 ==0 || (year % 100 ==0 && year % 4 ==0)) {
                regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$"                   options:NSRegularExpressionCaseInsensitive error:nil];// 测试出生日期的合法性
            }else {
                regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$"           options:NSRegularExpressionCaseInsensitive error:nil];// 测试出生日期的合法性
            }
            numberofMatch = [regularExpression numberOfMatchesInString:value options:NSMatchingReportProgress range:NSMakeRange(0, value.length)];
            if(numberofMatch > 0) {
                return YES;
            }else {
                return NO;
            }
        case 18:
            year = [value substringWithRange:NSMakeRange(6,4)].intValue;
            if (year % 4 ==0 || (year % 100 ==0 && year % 4 ==0)) {
                regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19|20[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$"options:NSRegularExpressionCaseInsensitive error:nil];// 测试出生日期的合法性
                
            }else {
                regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19|20[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$"
                                                                         options:NSRegularExpressionCaseInsensitive error:nil];// 测试出生日期的合法性
            }
            numberofMatch = [regularExpression numberOfMatchesInString:value options:NSMatchingReportProgress range:NSMakeRange(0, value.length)];
            if(numberofMatch > 0) {
                int S = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7 + ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9 + ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10 + ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5 + ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8 + ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4 + ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2 + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6 + [value substringWithRange:NSMakeRange(9,1)].intValue *3;
                int Y = S % 11;
                NSString *M = @"F";
                NSString *JYM = @"10X98765432";
                M = [JYM substringWithRange:NSMakeRange(Y,1)]; // 判断校验位
                if ([M isEqualToString:[[value substringWithRange:NSMakeRange(17,1)] uppercaseString]]) {
                    return YES;// 检测ID的校验位
                }else {
                    return NO;
                }
            }else {
                return NO;
            }
            
        default:
            return NO;
    }
    return NO;
}


-(NSString *)timeFormatter:(double)secStr andFormatterStr:(NSString *)formatterStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:formatterStr];
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:secStr/1000.0];
    NSString *timeStr = [formatter stringFromDate:date];
    
    
    return timeStr;
}

-(NSString *)time:(NSDate *)date andFormatterStr:(NSString *)formatterStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:formatterStr];
    NSString *timeStr = [formatter stringFromDate:date];
    return timeStr;
}


-(void)toLoginVC
{
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    LoginViewController *login = [main instantiateViewControllerWithIdentifier:@"LoginNav"];
//    [self.navigationController pushViewController:login animated:YES];
    
    [self presentViewController:login animated:YES completion:^{
        
    }];
}

//-(id)toLeaveMessageVC
//{
//    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//    LeaveMessageVC *leaveMessageVC = [main instantiateViewControllerWithIdentifier:@"LeaveMessageVC"];
//    [self.navigationController pushViewController:leaveMessageVC animated:YES];
//    return leaveMessageVC;
//}


-(UIView *)createNoDataTipView
{
    UIView *view=[[NSBundle mainBundle]loadNibNamed:@"NoDataTipView" owner:nil options:nil][0];
    view.frame=CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    
    return view;
}

-(NSString *)getNowDateString
{
    return [NSString stringWithFormat:@"%.0f",[NSDate timeIntervalSinceReferenceDate]*1000000];
}

-(void)initMutDicAndArray
{
    
}
-(void)initViews
{
    
}
-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    
    view.backgroundColor = [UIColor clearColor];
    
    [tableView setTableFooterView:view];
}
-(NSString *)removeSpaceWithString:(NSString *)string
{
    NSString *strUrl = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    return strUrl;
}
-(BOOL)shouldAutorotate
{
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}


@end
